<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{ name: 'login' }">登入</router-link>
          </li>
          <li class="layui-this">
            找回密码
            <!--重置密码-->
          </li>
        </ul>
        <div
          class="layui-form layui-tab-content"
          id="LAY_ucm"
          style="padding: 20px 0"
        >
          <validation-observer ref="observer" v-slot="{ validate }">
            <div class="layui-tab-item layui-show">
              <div class="layui-form layui-form-pane">
                <form method="post">
                  <div class="layui-form-item">
                    <label for="L_username" class="layui-form-label"
                      >邮箱</label
                    >
                    <validation-provider
                      name="username"
                      rules="required|username"
                      v-slot="{ errors }"
                    >
                      <div class="layui-input-inline">
                        <input
                          type="text"
                          id="L_username"
                          name="username"
                          v-model="username"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-form-mid">
                        <span style="color: #c00">{{ errors[0] }}</span>
                      </div>
                    </validation-provider>
                  </div>

                  <div class="layui-form-item">
                    <validation-provider
                      name="code"
                      ref="coderr"
                      rules="required|length:4"
                      v-slot="{ errors }"
                    >
                      <div class="layui-row">
                        <label for="L_vercode" class="layui-form-label"
                          >验证码</label
                        >
                        <div class="layui-input-inline">
                          <input
                            type="text"
                            name="code"
                            v-model="code"
                            placeholder="请输入验证码"
                            autocomplete="off"
                            class="layui-input"
                          />
                        </div>
                        <div class>
                          <span
                            class="svg"
                            style="color: #c00"
                            @click="_getCode()"
                            v-html="svg"
                          ></span>
                        </div>
                      </div>
                      <div class="layui-form-mid">
                        <span style="color: #c00">{{ errors[0] }}</span>
                      </div>
                    </validation-provider>
                  </div>

                  <div class="layui-form-item">
                    <button
                      id="eventquery"
                      type="button"
                      class="layui-btn"
                      :disabled="isDisabled"
                      @click="validate().then(submit)"
                      @keyup.enter="validate().then(submit)"
                    >
                      提交
                    </button>
                  </div>
                </form>
              </div>
            </div>
          </validation-observer>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getCode, forget } from '@/api/login'
import mixin from '@/mixin/code'
import {
  ValidationProvider,
  ValidationObserver,
} from 'vee-validate'
export default {
  name: 'forget',
  mixins: [mixin],
  components: {
    ValidationProvider,
    ValidationObserver,
  },
  data () {
    return {
      svg: '',
      username: '',
      code: '',
      errorMsg: [],
      isDisabled: false
    }
  },
  //  props: {
  //   recommends: {
  //     type: Object,
  //     default() {
  //       return {

  //       }
  //     }
  //   }
  // },
  mounted () {

  },
  methods: {
    async submit () {
      const isValid = await this.$refs.observer.validate()
      if (!isValid) {
        // ABORT!!
        return
      }
      forget(
        {
          username: this.username,
          code: this.code,
          sid: localStorage.getItem('sid')
        }).then((res) => {
          console.log(res);
          if (res.code === 200) {
            this.$alert(res.msg)
            this._getCode()
            //设置定时器防止多发
            this.isDisabled = true
            setTimeout(() => {
              this.isDisabled = false
            }, 30000)

          } if (res.code == 500) {
            this.$alert('请检查用户名后重新发送邮件')
          }
          if (res.code == 401) {
            this.$refs.coderr.setErrors([res.msg])
            this._getCode()
          }
        })
    }
  }

}


</script>
<style lang="scss" scoped>
</style>